import personalService from '../../service/personal.service'
import syllabus from '../../service/index.service'
import groupApi from '../../service/group.service'
import config from '../../config'
import pkService from '../../service/pk.service'
const app = getApp()

Page({

  data: {
    userId: '',
    userName: '',
    userScore: 0,
    userRankNum: 0,
    userLevel: 1,
    userRoleDic: '',
    userImage: '',
    onPopup: false,
    role:'',
    manageIndexs: [4,5], // 属于管理组的对应序号
    userImage: '',

    showGroupPop:false,
    loadMore: 'over',
    loadContent: [
      '马不停蹄加载更多数据中...',
      '-- 已经到底了，加不了咯 --',
    ],
    classesListData:[],
    isExportStudents:false,
    showLimuQrCodePic:false,
    limuQrCodePic:'',
    pkSource: '',
    pkTarget: '',
    pkSort: 0,
    pkId: 0,
    myPkModal: false
  },

  onLoad() {
    this.setData({
      userId: app.globalData.userInfo.userId,
      role: app.globalData.userInfo.role,
      userName: app.globalData.userInfo.userName,
      userScore: app.globalData.userInfo.score,
      userRankNum: app.globalData.userInfo.ranking,
      userLevel: app.globalData.userInfo.level,
      userRoleDic: app.globalData.userInfo.roleDic,
      userImage: app.globalData.userInfo.image
    })
  },

  onShow() {
    this.getUserInfo();
    this.getMyPK();
  },

  /**
   * 切换报名列表/班级人员列表浮层
   */
  onPopupToggle() {
    this.setData({
      onPopup: !this.data.onPopup
    })
  },

  //创建钉钉群
  createDingGroup() {
     if (app.methods.enableButtonClicked()) {
       console.log("1111");
       this.onPopupToggle();
    }
  },

  // 菜单点击对应事件
  menuOnClick () {
    if (app.methods.enableButtonClicked()) {
        dd.showToast({
        type: 'none',
        content: '研发小哥正在加班加点 敬请期待。。。'
      })
    }
  },

  //getLiMuPic 获取立木积分核销劵
  getLiMuPic(){
    let that = this
    that.setData({
        limuQrCodePic: config.domain + '/limu/createLMERPic?userId=' +app.globalData.userInfo.id,
        showLimuQrCodePic: true
    })
  },

  
  //limuQrCodePicClose 立木积分核销劵关闭
  limuQrCodePicClose(){
    let that = this
    that.setData({
          showLimuQrCodePic: false
      })
  },

 

  /**
   * 签到
   */
  sign(opts) {
    dd.showLoading()

    let params = {
      courseId: opts && opts.courseId ? opts.courseId : '',
      teacherDingId: app.globalData.userInfo.userId,
      studentUserId: opts && opts.studentUserId ? opts.studentUserId : ''
    }

    let success = function (res) {
      dd.showToast({
        type: 'success',
        content: '签到成功'
      })
    }

    let fail = function (msg) {
       dd.showToast({
        type: 'fail',
        content: msg
      })
    }

    setTimeout(function() {
      dd.hideLoading()
      syllabus.sign(params, success, fail)
    }, 800)
  },

  
  //导出人员
  exportStudents() {
    this.setData({
      showGroupPop: true,
      isExportStudents:true
    });
    syllabus.classesList({}, (res)=> {
      this.setData({
        classesListData: res.data.result.datas
      });
    })
  },

  //合班建群

  createGroup() {
    this.setData({
      showGroupPop: true,
      isExportStudents:false
    });
    syllabus.classesList({}, (res)=> {
      this.setData({
        classesListData: res.data.result.datas
      });
    })
  },

  onItemClick(e) {
    const id = e.target.dataset.id;
    const data = this.data.classesListData.map(d=> {
      if(d.id === id) {
        d.checked = typeof d.checked === undefined ? true : !d.checked;
      }
      return d;
    });
    this.setData({
      classesListData:data
    });
  },

  onConfirmCreateGroup() {
    const self = this;
    const {isExportStudents,classesListData,userId} = this.data;
    const classIds = classesListData.filter(d=> d.checked).map(d=> isExportStudents ? d.indexNum : d.id );
    if(!classIds.length) {
      dd.showToast({
        type: 'none',
        content: '请选择班级'
      })
      return;
    };

    const type = isExportStudents ? 'exportStudents' : 'createGroup';
    const params = {classIds,dingTalkUserId:userId};

    let success = function (res) {
      dd.showToast({
        type: 'none',
        content: isExportStudents ? '导出成功' : '建群成功,请到钉钉查看'
      })
      self.onPopupClose();
    }

    let fail = function (msg) {
      dd.showToast({
          type: 'fail',
          content: msg
      })
    }
    
    groupApi[type](params, success, fail);
  },

  onPopupClose() {
    this.setData({
      showGroupPop: false,
    });
  },

  // 获取用户信息
  getUserInfo() {

    let params = {
      id: app.globalData.userInfo.id,
      tenantId:1
    }

    const self = this;
    let success = function (res) {
      self.setData({
          userScore: res.data.result.integral,
          userRankNum: res.data.result.integralRanking
      })
    }

    let fail = function (msg) {
      dd.showToast({
        type: 'fail',
        content: msg
      })
    }
    personalService.getUserInfo(params, success, fail)
  },
  myPKClose(){
    this.setData({
      myPkModal: false
    });
  },

  handlerPK(e){
    
     const self = this;
    let params = {
      id: self.data.pkId,
      status: e.target.dataset.status,
      tenantId:1
    }

    let contentStr = "您接受本次PK，请找教练监督完成pk!";
    if(e.target.dataset.status === "已拒绝"){
      contentStr = "您拒绝本次PK，稳住发育不要浪!";
    }
   
    let success = function (res) {
      dd.showToast({
        type: '提示',
        content: contentStr
      })
    }

    let fail = function (msg) {
      dd.showToast({
        type: 'fail',
        content: msg
      })
    }
    pkService.updatePkStatus(params, success, fail)
    this.myPKClose()
  },
  
   // 获取PK信息
  getMyPK() {
    let params = {
      id: app.globalData.userInfo.id,
      tenantId:1
    }

    const self = this;
    let success = function (res) {
    
      if(res.data.result != null && res.data.result.status == "发起中"){
          self.setData({
              pkSource: res.data.result.sourceName,
              pkTarget: res.data.result.targetName,
              pkSort: res.data.result.integral,
              pkId: res.data.result.id,
              myPkModal: true
          })
      }
    }

    let fail = function (msg) {
      dd.showToast({
        type: 'fail',
        content: msg
      })
    }
    pkService.getMyPK(params, success, fail)
  },
  
  // 操作跳转对应路由页面
  goToRouter(e) {
    let that = this

    let type = e.target.dataset.type

    if (type == 'score-rank-page') { // 跳转积分排名页面
      dd.reLaunch({ url: '/page/jianghu/jianghu?type=activeTab&value=1' })
    }
  }
})

